<?php if (!defined('THINK_PATH')) exit();?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>基础表单</title>
    <link rel="stylesheet" href="/Public/easyweb/assets/libs/layui/css/layui.css"/>
    <link rel="stylesheet" href="/Public/easyweb/assets/module/admin.css?v=318"/>
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        #formBasForm {
            max-width: 700px;
            margin: 30px auto;
        }

        #formBasForm .layui-form-item {
            margin-bottom: 25px;
        }
		
		body .tox-tinymce-aux {
            z-index: 19892000;
        }
		
		#formBasForm{ max-width:1000px;}
    </style>
</head>
<body>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表单开始 -->
            <form class="layui-form model-form" id="formBasForm" lay-filter="formBasForm">
				<input name="userId" type="hidden"/>
                <div class="layui-form-item">
                    <label class="layui-form-label layui-form-required">标题:</label>
                    <div class="layui-input-block">
                        <input name="title" placeholder="" class="layui-input"
                               lay-verType="tips" lay-verify="required" required/>
                    </div>
                </div>
				
				<div class="layui-form-item">
                    <label class="layui-form-label layui-form-required">正文内容:</label>
                    <div class="layui-input-block">
						<!-- <div class="layui-btn-container">
							<button id="btnDemoEdtGetContent" class="layui-btn layui-btn-sm">获取内容</button>
							<button id="btnDemoEdtGetText" class="layui-btn layui-btn-sm">获取纯文本</button>
						</div> -->

                        <textarea id="demoEditor"></textarea>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label layui-form-required">协议所属:</label>
                    <div class="layui-input-block">
                        <input type="radio" name="comefrom" lay-verify="comefrom" value="注册" title="注册">
                        <input type="radio" name="comefrom" lay-verify="comefrom" value="积分" title="积分">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-filter="formBasSubmit" lay-submit>&emsp;提交&emsp;</button>
                        <button type="reset" class="layui-btn layui-btn-primary">&emsp;重置&emsp;</button>
                    </div>
                </div>
            </form>
            <!-- //表单结束 -->
        </div>
    </div>
</div>

<!-- js部分 -->
<script type="text/javascript" src="/Public/easyweb/assets/libs/layui/layui.js"></script>
<script type="text/javascript" src="/Public/easyweb/assets/js/common.js?v=318"></script>
<script type="text/javascript" src="/Public/easyweb/assets/libs/tinymce/tinymce.min.js"></script>
<script>
    layui.use(['layer', 'form', 'laydate', 'admin', 'formX'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var laydate = layui.laydate;
		var admin = layui.admin;
		var formX = layui.formX;

		//admin.iframeAuto();//接收上个页面传递的参数数据
		var layerData = admin.getLayerData();
		//console.log(layerData);
		
		form.verify({
			comefrom: function(value){
			  if( typeof($('input[name="comefrom"]:checked').val()) == "undefined"){
				return '协议所属必选';
			  }
			}
		});
		
		// 渲染富文本编辑器
        tinymce.init({
            selector: '#demoEditor',
            height: 525,
            branding: false,
            language: 'zh_CN',
            plugins: 'code print preview fullscreen paste searchreplace save autosave link autolink image imagetools media table codesample lists advlist hr charmap emoticons anchor directionality pagebreak quickbars nonbreaking visualblocks visualchars wordcount',
            toolbar: 'fullscreen preview code | undo redo | forecolor backcolor | bold italic underline strikethrough | alignleft aligncenter alignright alignjustify | outdent indent | numlist bullist | formatselect fontselect fontsizeselect | link image media emoticons charmap anchor pagebreak codesample | ltr rtl',
            toolbar_drawer: 'sliding',
            //images_upload_url: '/Public/easyweb/json/tinymce-upload-ok.json',
			images_upload_url: '<?php echo U("Userlistapp/tinymce_uploadImg?lujing=xieyi");?>',
            file_picker_types: 'media',
            file_picker_callback: function (callback, value, meta) {
                layer.msg('演示环境不允许上传', {anim: 6});
            },
            init_instance_callback: function (editor) {
                //console.log(editor);
				//判断是添加还是修改
				if( layerData.userId ){
					// 回显表单数据
					var content = layerData.content;
					tinymce.get('demoEditor').insertContent(content, {});
				}
            }
        });

		//~~ 带这个标志的都是之前的老写法 也能用 
		//判断是添加还是修改
		/*var data_url = null;
		if( layerData.userId ){
			// 回显表单数据
			//form.val('formBasForm', layerData);
			form.val('formBasForm', JSON.parse(JSON.stringify(layerData)));

			data_url = "<?php echo U('Userlistapp/xieyi_Edit');?>";
		}else{
			data_url = "<?php echo U('Userlistapp/xieyi_insert');?>";
		}*/
		
		// 回显数据，这里一定要用formX.val，form.val不能回显跨iframe的数据
		formX.val('formBasForm', layerData);
		
        /* 监听表单提交 */
        form.on('submit(formBasSubmit)', function (data) {
			data.field.r_postcode = layerData.r_postcode;	
			data.field.content = tinymce.get('demoEditor').getContent();
			//console.log(data.field);
			
			layer.load(2);
			var data_url = layerData.userId ? "<?php echo U('Userlistapp/xieyi_Edit');?>" : "<?php echo U('Userlistapp/xieyi_insert');?>";
			$.post(data_url, {data: data.field}, function (res) {
                layer.closeAll('loading');
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    admin.putLayerData('formOk', true);  // 设置操作成功的标识
                    admin.closeThisDialog();  // 关闭当前iframe弹窗
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'json');
            return false;
        });

    });
</script>
</body>
</html>